<script setup lang='ts'>
import { ref, onMounted } from "vue"
import { useRouter } from 'vue-router'
import { useCounterStore } from '../stores/counter'
import { getBookChapterList, getPublishBookChapterList } from "@/api/index"
import { showFailToast } from 'vant';
import LoadingPage from "@/components/LoadingPage.vue"
const isLoadingPage = ref<boolean>(true)
const store = useCounterStore()

const $router = useRouter()
const isReverse = ref<boolean>(false)
const reverseChapterList = ref<any>([])

const bookChapterList = ref<any>([])

onMounted(async () => {
    let querys = $router.currentRoute.value.query

    let bookChapterRes = await getBookChapterList(querys.uuid)
    if (bookChapterRes.data.code == -1) {

        let bookPubliskCahpterRes = await getPublishBookChapterList(querys.uuid)
        bookChapterList.value = bookPubliskCahpterRes.data.ncx.navMap.navPoint
        bookChapterList.value.forEach((o: any) => {
            if (o.navPoint?.length > 0) {

                reverseChapterList.value = [...reverseChapterList.value, o]
                reverseChapterList.value = [...reverseChapterList.value, ...o.navPoint]

            } else {
                reverseChapterList.value = [...reverseChapterList.value, o]
            }

        })
    } else {
        bookChapterList.value = bookChapterRes.data.data.catalog
        reverseChapterList.value = bookChapterList.value
    }
    store.getChapter(reverseChapterList.value)

    isLoadingPage.value = false
})

const toggleType = () => {

    isReverse.value = !isReverse.value
    if (isReverse) {
        return reverseChapterList.value.reverse()
    } else {
        return reverseChapterList.value
    }
}

const toContentPage = (item: any, index: number) => {
    if (bookChapterList.value[index].needPay || bookChapterList.value[index].vip == 1) {
        console.log(11);

        showFailToast('付费内容');
    } else {
        $router.push({
            path: '/content', query: {
                source_uuid: $router.currentRoute.value.query.uuid, content_uuid: item.uuid || item.id, title: $router.currentRoute.value.query.title, index: index
            }
        })
    }

}
</script>

<template>
    <div class="chapter">
        <div class="chapter-title">
            <img src="@/assets/imgs/返回1.png" alt="" @click="$router.back()">
            <span>目录</span>
            <span></span>
        </div>
        <div class="xx">
            <LoadingPage v-if="isLoadingPage" />
            <div class="yy" v-else>
                <div class="author-msg">
                    <div class="img">
                        <img :src="`${$router.currentRoute.value.query.avator}?imageView&thumbnail=240y336`" alt="">
                    </div>
                    <div class="msg">
                        <span>{{ $router.currentRoute.value.query.title }}</span>
                        <span>{{ $router.currentRoute.value.query.author }}</span>
                    </div>
                </div>
                <div class="chapter-list">
                    <div class="all-reverse">
                        <span>共{{ reverseChapterList.length }}章</span>
                        <div class="reverse" @click="toggleType">
                            <span v-if="!isReverse">正序 </span>
                            <span v-if="isReverse">倒序 </span>
                            <img src="@/assets/imgs/向上箭头3.png" alt="" v-if="isReverse">
                            <img src="@/assets/imgs/向下箭头2.png" alt="" v-if="!isReverse">
                        </div>
                    </div>
                    <ul class="list">
                        <li v-for="item, index in reverseChapterList" :key="index" @click="toContentPage(item, index)">
                            <span>{{ item.title || item.navLabel?.slice(1, item.navLabel.length) }}</span>
                            <img src="@/assets/imgs/锁2.png" alt="" v-if="item.needPay || item.vip == 1">
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</template>

<style lang="less" scoped>
.chapter {
    padding-top: 15rem;
    height: 100vh;

    .chapter-title {
        padding: 0 10rem 15rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #eee;

        img {
            width: 25rem;
            height: 25rem;
        }

        span {
            color: #333;
            font-size: 17rem
        }
    }

    .xx {
        width: 100%;
        height: calc(100vh - 58rem);

        .yy {
            .author-msg {
                padding: 20rem 10rem;
                display: flex;
                justify-content: left;
                align-items: center;

                .img {
                    margin-right: 20rem;

                    img {
                        width: 70rem;
                        height: 98rem;
                        box-shadow: 0 0 5rem 1rem #ccc;
                    }
                }

                .msg {
                    display: flex;
                    flex-direction: column;

                    span {
                        &:first-child {
                            font-size: 16rem;
                            color: #333;
                            margin-bottom: 10rem;
                        }
                    }
                }
            }

            .chapter-list {
                padding: 0 10rem;

                .all-reverse {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;

                    >span {
                        font-size: 15rem;
                        color: #333;
                    }

                    .reverse {
                        display: flex;
                        align-items: center;
                        padding: 0 6rem;
                        border: 1rem solid #ccc;
                        border-radius: 20rem;

                        span {
                            font-size: 14rem;
                            margin-right: 3rem;
                        }

                        img {
                            width: 10rem;

                        }
                    }
                }

                ul.list {
                    margin-top: 10rem;
                    height: 435rem;
                    overflow: auto;

                    li {
                        padding: 10rem 0;
                        border-bottom: 1px dotted #eee;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;

                        span {
                            font-size: 14rem;
                        }

                        img {
                            width: 20rem;
                        }
                    }
                }
            }
        }
    }

}
</style>
